/**
 * 业务公共处理类
 * @author luolin
 */
import { validateNull } from "@/utils/validate";

// 处理克隆
export function handleClone(operType, form: any) {
  if (operType !== "add" && operType !== "copy") return;
  if (!form) return;
  setPropsNull(
    form,
    "orderInstCode",
    "flowInstId",
    "status",
    "finishTime",
    "createUser",
    "createTime",
    "updateUser",
    "updateTime"
  );
  // 自定义清空字段
  const args = Array.prototype.slice.call(arguments);
  setPropsNull(form, ...args.slice(1));
  return form;
}

// 处理克隆提交前
export function handleCloneSubmit(
  operType,
  row?: any,
  callback?: (params: object) => void
) {
  if (operType !== "add" && operType !== "copy") return;
  if (!row) return;
  row = handleClone(operType, row);
  setPropsNull(row, "id", "flowInstId");
  if (callback) callback(row);
}

// 设置属性值null
// @ts-ignore
export function setPropsNull(form: any, ...keys: string[]) {
  if (arguments.length < 2) return;
  for (let i = 0; i < keys.length; i++) {
    form[keys[i]] = null;
  }
}

// 统一设置属性值
// @ts-ignore
export function setPropsValue(form: any, value: any) {
  if (arguments.length < 3) return;
  const args = Array.prototype.slice.call(arguments);
  for (let i = 2; i < args.length; i++) {
    form[args[i]] = value;
  }
}

// 设置属性为data值
// @ts-ignore
export function setPropsDataValue(form, data) {
  if (arguments.length < 3) {
    const keys = Object.keys(data);
    for (let i = 2; i < keys.length; i++) {
      if (!validateNull(data[keys[i]])) form[keys[i]] = data[keys[i]];
    }
    return;
  }
  const args = Array.prototype.slice.call(arguments);
  // 第三个参数是否为数组
  if (args[2] instanceof Array) {
    args[2].forEach(key => {
      form[key] = data[key];
    });
  } else {
    for (let i = 2; i < args.length; i++) {
      if (!validateNull(data[args[i]])) form[args[i]] = data[args[i]];
    }
  }
}
